package com.xiaoming.moj.judge.codesandbox.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.util.StringUtils;
import com.xiaoming.moj.exception.BusinessException;
import com.xiaoming.moj.judge.codesandbox.CodeSandBox;
import com.xiaoming.moj.judge.codesandbox.model.ExecuteCodeRequest;
import com.xiaoming.moj.judge.codesandbox.model.ExecuteCodeResponse;

/**
 * 远程代码沙箱
 */
public class RemoteSandBox implements CodeSandBox {
    private static final String AUTH_REQUEST_HEADER="auth";
    private static final String AUTH_REQUEST_STR="xiaoming";

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        String url="http://localhost:8092/executeCode";
        String json= JSONUtil.toJsonStr(executeCodeRequest);
        String responseStr = HttpUtil.createPost(url)
                .header(AUTH_REQUEST_HEADER,AUTH_REQUEST_STR)
                .body(json)
                .execute()
                .body();
        if(StringUtils.isBlank(responseStr))
        {
            throw new BusinessException(50010,"远程代码沙箱错误,message={}"+responseStr);
        }

        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
